<template>
  <div>
    <Table
      ref="dataTable"
      :loading="table.loading"
      :columns="table.tableColumns"
      :data="table.tableRows"
      style="margin-top:20px;"
      @on-select="select" @on-select-cancel="selectCancel"
               @on-select-all="selectAll" @on-select-all-cancel="selectAllCancel"
      @on-sort-change="changeSort"
      :row-class-name="rowClassName"
    ></Table>
    <div style="margin: 20px; overflow: hidden">
      <div style="float: right;">
        <Page
          :total="pager.total"
          :current="pager.pageNo"
          @on-change="changePageNo"
          @on-page-size-change="changePageSize"
          showSizer
          showTotal
        ></Page>
      </div>
    </div>
  </div>
</template>

<script>
import * as utils from '@/api/utils-v2'

export default {
  name: 'GoodsOrderTableMain',
  props: {
    isVerification: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      urls: {
        searchUrl: '/goods-order/admin/pager-cond',
        activeUrl: '/goods-order/admin/active',
        removeUrl: '/goods-order/admin/remove/'
      },
			dataDictCodes: ['IS_ACTIVE','PAYMENT_TYPE','GOODS_PAYSUCCESS','GOODS_ORDER_STATUS'],
			dataDictItems: {},
      pager: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      },
      searchOpts: {
        sortColumn: null,
        sortOrder: null
      },
      table: {
        loading: false,
        tableColumns: [
          {
            type: 'selection',
            width: 52,
            key: 'id',
            align: 'center',
            fixed: 'left'
          },
          {
            width: 60,
            align: 'center',
            fixed: 'left',
            render: (h, params) => {
              return h(
                'span',
                params.index + (this.pager.pageNo - 1) * this.pager.pageSize + 1
              )
            }
          },
          {
            title: '订单编号',
            key: 'id',
            minWidth: 120,
            sortable: true
          },
          {
            title: '用户编号',
            key: 'userId',
            minWidth: 120,
            sortable: true,
            render: (h, params) => {
						  return h(
						    'Dropdown',
						    {
						      on: {
						        'on-click': itemName => {
						          this.userOpt(itemName, params.row)
						        }
						      },
						      props: {
						        transfer: true
						      }
						    },
						    [
						      h('span', [
						        params.row.userId,
						        h('Icon', {
						          props: {
						            type: 'ios-list',
						            size: '25'
						          }
						        })
						      ]),
						      h(
						        'DropdownMenu',
						        {
						          slot: 'list'
						        },
						        [
						          h(
						            'DropdownItem',
						            {
						              props: {
						                name: 'userModuleDetail'
						              }
						            },
						            '详情'
						          ),
						          h(
						            'DropdownItem',
						            {
						              props: {
						                name: 'showUserSearch'
						              }
						            },
						            '搜索'
						          )
						        ]
						      )
						    ]
						  )
						}
          },
          {
title: '店铺编号',
key: 'shopId',
minWidth: 120,
sortable: true,
},
          {
            title: '订单号',
            key: 'orderNo',
            minWidth: 120,
            sortable: true
          },
          {
            title: '订单金额（元）',
            key: 'totalAmount',
            minWidth: 120,
            sortable: true
          },
          {
            title: '实付金额（元）',
            key: 'payAmount',
            minWidth: 120,
            sortable: true
          },
          {
            title: '优惠金额（元）',
            key: 'discountAmount',
            minWidth: 120,
            sortable: true
          },
          {
            title: '赠送积分',
            key: 'integralAmount',
            minWidth: 120,
            sortable: true
          },
          {
            title: '运费（元）',
            key: 'expressFee',
            minWidth: 120,
            sortable: true
          },
          {
            title: '订单状态',
            key: 'orderStatus',
            minWidth: 120,
            sortable: true,
			render: (h, params) => {
			        let dictItem = utils.getDataDictItemDisplayValue(this, 'GOODS_ORDER_STATUS', params.row.orderStatus)
			     
					return h('Button', {
					  props: {
					    type: dictItem.color ==""? 'default':'primary',
					    size: 'small'
					  },
					  on: {
					    'click': () => {
					      this.showDeliverGoods(params.row)
					    }
					  }
					}, dictItem.displayValue)
			 },
          },
          {
            title: '支付时间',
            key: 'payTime',
            minWidth: 120,
            sortable: true
          },
          {
            title: '支付方式',
            key: 'payType',
            minWidth: 120,
            sortable: true,
			render: (h, params) => {
			        let dictItem = utils.getDataDictItemDisplayValue(this, 'PAYMENT_TYPE', params.row.payType)
			        return h('span', dictItem.displayValue)
			 }
          },
          {
            title: '支付订单号',
            key: 'transactionNo',
            minWidth: 120,
            sortable: true
          },
          {
            title: '是否支付成功',
            key: 'paySuccess',
            minWidth: 120,
            sortable: true,
			render: (h, params) => {
			        let dictItem = utils.getDataDictItemDisplayValue(this, 'GOODS_PAYSUCCESS', params.row.paySuccess)
			        return h('span', dictItem.displayValue)
			 }
          },
          {
            title: '发货时间',
            key: 'deliverTime',
            minWidth: 120,
            sortable: true
          },
          {
            title: '成交时间',
            key: 'dealTime',
            minWidth: 120,
            sortable: true
          },
          {
            title: '订单备注',
            key: 'remark',
            minWidth: 120,
            sortable: true
          },
          {
            title: '商家备注',
            key: 'shopRemark',
            minWidth: 120,
            sortable: true
          },
          {
            title: '版本号',
            key: 'version',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '版本号'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '订单版本号',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '创建时间',
            key: 'createTime',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '创建时间'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '订单创建时间',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '更新时间',
            key: 'updateTime',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '更新时间'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '订单更新时间',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            }
          },
          {
            title: '是否激活',
            key: 'isActive',
            minWidth: 120,
            sortable: true,
            renderHeader: (h, params) => {
              return h('span', [
                h('span', '是否激活'),
                h(
                  'Tooltip',
                  {
                    props: {
                      content: '订单是否激活',
                      placement: 'top',
                      transfer: true,
                      maxWidth: 500
                    }
                  },
                  [
                    h('Icon', {
                      props: {
                        type: 'ios-help-circle'
                      },
                      style: {
                        marginLeft: '3px'
                      }
                    })
                  ]
                )
              ])
            },
			render: (h, params) => {
			        let dictItem = utils.getDataDictItemDisplayValue(this, 'IS_ACTIVE', params.row.isActive)
			        return h('span', dictItem.displayValue)
			 }
          },

          {
            title: '激活状态',
            key: 'isActive',
            minWidth: 100,
            align: 'center',
            render: (h, params) => {
              if (params.row.isActive === 0) {
                return h('Tooltip', {
                  props: {
                    content: '点击冻结',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'success',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.active(params.row)
                      }
                    }
                  }, '激活')
                ])
              } else {
                return h('Tooltip', {
                  props: {
                    content: '点击激活',
                    placement: 'top',
                    transfer: true,
                    maxWidth: 500
                  }
                }, [
                  h('Button', {
                    props: {
                      type: 'error',
                      size: 'small'
                    },
                    on: {
                      'click': () => {
                        this.active(params.row)
                      }
                    }
                  }, '冻结')
                ])
              }
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 130,
            align: 'center',
            fixed: 'right',
            render: (h, params) => {
              return h(
                'Dropdown',
                {
                  on: {
                    'on-click': itemName => {
                      this.userOpt(itemName, params.row)
                    }
                  },
                  props: {
                    transfer: true
                  }
                },
                [
                  h(
                    'Button',
                    {
                      props: {
                        type: 'primary',
                        size: 'small'
                      }
                    },
                    [
                      '选择操作 ',
                      h('Icon', {
                        props: {
                          type: 'ios-arrow-down'
                        }
                      })
                    ]
                  ),
                  h(
                    'DropdownMenu',
                    {
                      slot: 'list'
                    },
                    [
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showEdit'
                          },
                          style: {
                            display: !this.isVerification ? 'block' : 'none'
                          }
                        },
                        '编辑'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDetail'
                          }
                        },
                        '详情'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'remove'
                          },
                          style: {
                            display: !this.isVerification ? 'block' : 'none'
                          }
                        },
                        [
                          h(
                            'span',
                            {
                              style: {
                                color: 'red'
                              }
                            },
                            '删除'
                          )
                        ]
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'deliverGoods'
                          },
                          style: {
                            display: params.row.orderStatus === 3 && !this.isVerification ? 'block' : 'none'
                          }
                        },
                        '发货'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDeliverGoods'
                          },
                          style: {
                            display: params.row.orderStatus === 3 && !this.isVerification ? 'none' : 'block'
                          }
                        },
                        '发货信息'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showOrderItem'
                          },
                          style: {
                            display: !this.isVerification ? 'block' : 'none'
                          }
                        },
                        '查看订单项'
                      ),
                    ]
                  )
                ]
              )
            }
          }
        ],
        tableRows: [],
        selections: []
      }
    }
  },
  computed: {},
  mounted() {
    this.search()
  },
  methods: {
    search() {
      this.$emit('searchTable')
    },
    userOpt(itemName, row) {
      if (itemName === 'showEdit') {
        this.$emit('showEditModal', JSON.parse(JSON.stringify(row)))
      } else if (itemName === 'showDetail') {
        this.$emit('showDetailModal', JSON.parse(JSON.stringify(row)))
      } else if (itemName === 'remove') {
        utils.remove(this, row)
      } else if (itemName === 'userModuleDetail') {
        this.$emit('showAttrDetailModal', row.userId)
      } else if (itemName === 'showUserSearch') {
        this.$emit('showSearchTableModal')
      } else if (itemName === 'deliverGoods') {
        // 发货
        this.showDeliverGoods(row)
      } else if (itemName === 'showDeliverGoods') {
        this.showDeliverGoods(row)
      } else if (itemName === 'showOrderItem') {
        // 查看订单项
        this.$emit('showOrderItem', row)
      }
    },
    active(row) {
      utils.active(this, row)
    },
    select(selections, row) {
              utils.select(this, selections, row)
            },
            selectCancel(selections, row) {
              utils.selectCancel(this, selections, row)
            },
            selectAll(selections) {
              utils.selectAll(this, selections)
            },
            selectAllCancel(selections) {
              utils.selectAllCancel(this, selections)
            },
    changeSort(sortColumn) {
      utils.changeSort(this, sortColumn)
    },
    changePageNo(pageNo) {
      utils.changePageNo(this, pageNo)
    },
    changePageSize(pageSize) {
      utils.changePageSize(this, pageSize)
    },
    rowClassName (row, index) {
      if (row.orderStatus === 3) {
        return 'wait-delivery-row'
      }
      return ''
    },
    showDeliverGoods(row) {
      this.$emit('showDeliverGoods', row)
    }
  }
}
</script>

<style>
  .ivu-table-body .wait-delivery-row td {
    background-color: #2db7f5;
    color: #fff;
  }

  .ivu-table-body .wait-payment td {
    background-color: #ff6600;
    color: #fff;
  }
</style>
